clear all

cd "/Users/TJRyan/Library/CloudStorage/Dropbox/2022 Primary Survey/POBE Replication files"

**#  Wisconsin
use "1_rawdata/wi_raw.dta", clear

gen startdate3 = substr(startdate, 1, 10)
gen startdate2 = date(startdate3, "YMD")
drop startdate3
order startdate2, after(startdate)
format startdate2 %td

gen starttime = substr(startdate, 12, 19)
gen starttime2 = clock(starttime, "hms") / 1000
order starttime2, after(startdate2)
format starttime2 %14.0f // Seconds since midnight

gen late_response = 0
replace late_response = 1 if startdate2 > td(09aug2022) // Primary was August 9.
replace late_response = 1 if startdate2 == td(09aug2022) & starttime2>72000 // Polls closed 8pm Wisconsin = 20*60*60 = 72000 seconds since midnight.

gen votecand=""
	replace votecand="g1" if regexm(vote_gov_rep,"cand2")==1 & elect_showgov=="1" & party=="Republican"
	replace votecand="g2" if regexm(vote_gov_rep,"cand3")==1 & elect_showgov=="1" & party=="Republican"
	replace votecand="s1" if regexm(vote_sen_dem,"cand1")==1 & elect_showsen=="1" & party=="Democrat"
	replace votecand="s2" if regexm(vote_sen_dem,"cand2")==1 & elect_showsen=="1" & party=="Democrat"
	replace votecand="s3" if regexm(vote_sen_dem,"cand3")==1 & elect_showsen=="1" & party=="Democrat" 

local encode_list primaryparticipation primaryparty1 primaryparty2 primaryparty3 ///
	gender education income religion pid postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 ///
	postvote_gov_1 postvote_gov_2 elect_treatcond_sen elect_treatcond_gov party //
	
foreach var in `encode_list' {
	rename `var' `var'_old
	encode `var'_old, gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

destring electability_* elect_showsen, replace

numlabel, add	

recode educ (5=1) (4=2) (1=3) (8=4) (2=5) (6=6) (3 7=7), gen(educ2)
lab def educ2 1 "No Diploma" 2 "HS" 3 "Associates" 4 "Some col" 5 "BA" 6 "MA" 7 "Advanced degree"
lab val educ2 educ2
	
local val_order postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_gov_1 postvote_gov_2
foreach var in `val_order' {
	rename `var' `var'_old
	recode `var'_old (2=0 "0. Not at all likely") (3=1 "1. Not very likely ") (4=2 "2. Somewhat likely ") (1=3 "3. Certain to"), gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

recode primaryparticipation (3 4 6 = 1) (1 2 5 = 0), gen(voter)
lab def voter 0 "Not likely voter" 1 "Likely voter"
lab val voter voter	
	
rename mc_officegov mc_office_gov
rename q95 mc_moderate_gov
rename v139 mc_office_sen
rename q96 mc_money_sen
rename q97 mc_moderate_sen

rename vote_gov_rep vote_gov_rep2
gen vote_gov_rep = ""
replace vote_gov_rep = "Fischer" if regex(vote_gov_rep2, "cand1}")
replace vote_gov_rep = "Kleefisch" if regex(vote_gov_rep2, "cand2}")
replace vote_gov_rep = "Michels" if regex(vote_gov_rep2, "cand3}")
replace vote_gov_rep = "Nicholson" if regex(vote_gov_rep2, "cand4}")
replace vote_gov_rep = "Ramthun" if regex(vote_gov_rep2, "cand5}")

order vote_gov_rep, after(vote_gov_rep2)
drop vote_gov_rep2

rename vote_sen_dem vote_sen_dem2
gen vote_sen_dem = ""
replace vote_sen_dem = "Barnes" if regex(vote_sen_dem2, "cand1}")
replace vote_sen_dem = "Godlewski" if regex(vote_sen_dem2, "cand2}")
replace vote_sen_dem = "Lasry" if regex(vote_sen_dem2, "cand3}")
replace vote_sen_dem = "Lee" if regex(vote_sen_dem2, "cand4}")
replace vote_sen_dem = "Nelson" if regex(vote_sen_dem2, "cand5}")
replace vote_sen_dem = "Olikara" if regex(vote_sen_dem2, "cand6}")
replace vote_sen_dem = "Peckarsky" if regex(vote_sen_dem2, "cand7}")
replace vote_sen_dem = "Williams" if regex(vote_sen_dem2, "cand8}")

order vote_sen_dem, after(vote_sen_dem2)
drop vote_sen_dem2

rename electability_sen1_1 electability_sen1
rename electability_sen2_1 electability_sen2
rename electability_sen3_1 electability_sen3
rename electability_sen4_1 electability_sen4

rename electability_gov1_1 electability_gov1
rename electability_gov2_1 electability_gov2


foreach num of numlist 1/4 {
	gen t_exp_y_s`num' = 0
	gen t_exp_n_s`num' = 0
	gen t_money_y_s`num' = 0
	gen t_money_n_s`num' = 0
	gen t_moderate_y_s`num' = 0
	gen t_moderate_n_s`num' = 0
	}

foreach num of numlist 1/2 {
	gen t_exp_y_g`num' = 0
	gen t_exp_n_g`num' = 0
	gen t_money_y_g`num' = 0
	gen t_money_n_g`num' = 0
	gen t_moderate_y_g`num' = 0
	gen t_moderate_n_g`num' = 0
	}

/* Note:
elect_sencand1 = Mandela Barnes
elect_sencand2 = Sarah Godlewski
elect_sencand3 = Alex Lasry
elect_sencand4 = Tom Nelson */

replace t_exp_y_s1 = 1 if elect_treatcond_sen==2 // Barnes yes experience
replace t_exp_y_s2 = 1 if elect_treatcond_sen==2 // Godlewski yes experience
replace t_exp_n_s3 = 1 if elect_treatcond_sen==2 // Lasry no experience
replace t_exp_y_s4 = 1 if elect_treatcond_sen==2 // Nelson yes experience

replace t_moderate_n_s1 = 1 if elect_treatcond_sen==3 // Barnes not moderate
replace t_moderate_y_s2 = 1 if elect_treatcond_sen==3 // Godlewski yes moderate
replace t_moderate_n_s3 = 1 if elect_treatcond_sen==3 // Lasry not moderate
replace t_moderate_n_s4 = 1 if elect_treatcond_sen==3 // Nelson not moderate

replace t_money_n_s1 = 1 if elect_treatcond_sen==4 // Barnes no money
replace t_money_n_s2 = 1 if elect_treatcond_sen==4 // Godlewski no money
replace t_money_y_s3 = 1 if elect_treatcond_sen==4 // Lasry yes money
replace t_money_n_s4 = 1 if elect_treatcond_sen==4 // Nelson no money

/* Note:
elect_govcand1 = Rebecca Kleefish
elect_govcand2 = Tim Michels */

replace t_exp_y_g1 = 1 if elect_treatcond_gov==2 // Kleefish yes experience
replace t_exp_n_g2 = 1 if elect_treatcond_gov==2 // Michels no experience

replace t_moderate_y_g1 = 1 if elect_treatcond_gov==3 // Kleefish yes moderate
replace t_moderate_n_g2 = 1 if elect_treatcond_gov==3 // Michels not moderate

* Code MC as correct
foreach num of numlist 1/4 {
	gen mc_office_s`num' = 0
	gen mc_money_s`num' = 0
	gen mc_moderate_s`num' = 0
	gen perceived_money_s`num' = 0
	gen perceived_office_s`num' = 0
	gen perceived_moderate_s`num' = 0
	}	

foreach num of numlist 1/2 {
	gen mc_office_g`num' = 0
	gen mc_moderate_g`num' = 0
	gen perceived_office_g`num' = 0
	gen perceived_moderate_g`num' = 0
	}

gen govshow_gop = 0
gen senshow_dem = 0

replace govshow_gop = 1 if elect_showsen == 0
replace senshow_dem = 1 if elect_showsen == 1	
	
replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}")  // Barnes correct if present
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}")  // Godlewski correct if present
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") == 0 // Lasry correct if absent
replace mc_office_s4 = 1 if regexm(mc_office_sen, "cand4}")  // Nelson correct if present

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") == 0 // Barnes correct if absent
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") // Godlewski correct if present
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") == 0 // Lasry correct if absent
replace mc_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}") == 0 // Nelson correct if absent

replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") == 0 // Barnes correct if absent
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") == 0 // Godlewski correct if absent
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") // Lasry correct if present 
replace mc_money_s4 = 1 if regexm(mc_money_sen, "cand4}") == 0 // Nelson correct if absent

** Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") 
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}") 
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") 
replace perceived_office_s4 = 1 if regexm(mc_office_sen, "cand4}") 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}") 
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}") 
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}") 
replace perceived_money_s4 = 1 if regexm(mc_money_sen, "cand4}") 

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") 
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") 
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") 
replace perceived_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}") 

replace mc_office_g1 = 1 if regexm(mc_office_gov, "cand1}")  // Kleefish correct if present
replace mc_office_g2 = 1 if regexm(mc_office_gov, "cand2}") == 0 // Michels correct if absent

replace mc_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}")  // Kleefish correct if present
replace mc_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}") == 0 // Michels correct if absent

** Perceptions
replace perceived_office_g1 = 1 if regexm(mc_office_gov, "cand1}") 
replace perceived_office_g2 = 1 if regexm(mc_office_gov, "cand2}") 

replace perceived_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}") 
replace perceived_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}")

* Missing values for ones that were not shown
foreach num of numlist 1/4 {
	replace mc_office_s`num' = . if elect_showsen==0
	replace mc_moderate_s`num' = . if elect_showsen==0
	replace mc_money_s`num' = . if elect_showsen==0
	}

foreach num of numlist 1/2 {
	replace mc_office_g`num' = . if elect_showsen==1
	replace mc_moderate_g`num' = . if elect_showsen==1
	}

foreach num of numlist 1/4 {
	rename  electability_sen`num' electability_s`num'
	rename  postvote_sen_`num' postvote_s`num'
	}

foreach num of numlist 1/2 {
	rename  electability_gov`num' electability_g`num'
	rename  postvote_gov_`num' postvote_g`num'
	}
	
gen state = "WI"
gen race_fe = ""

replace race_fe = "WI_GOP_Governor" if elect_showsen==0
replace race_fe = "WI_Dem_Senate" if elect_showsen==1

gen post_candrop=1 if party==1 & startdate2 >= td(29jul2022) // As of July 28, all the candidates had dropped out and endorsed Barnes.

keep responseid t_* electability_* elect_treatcond_* party state mc_* postvote* ///
	race_fe vote_gov_rep vote_sen_dem govshow_gop senshow_dem perceived_* ///
	pid pid_leaner pid_strength_dem pid_strength_rep voter educ2  ///
	 post_candrop votecand late_response /// 
	gender education race birthyr ideology startdate 
	
drop mc_office_sen mc_money_sen mc_moderate_sen mc_office_gov mc_moderate_gov
reshape long electability_ t_exp_y_ t_exp_n_ t_money_y_ t_money_n_ t_moderate_y_ t_moderate_n_ mc_office_ mc_money_ mc_moderate_ postvote_ perceived_office_ perceived_money_ perceived_moderate_, i(responseid) j(cand) string

gen cand_fe = cand + "_rep" + "_WI" if party==2
replace cand_fe = cand + "_dem" + "_WI" if party==1

gen cand_name = ""
replace cand_name = "Barnes" if regexm(cand_fe, "s1_dem")
replace cand_name = "Godlewski" if regexm(cand_fe, "s2_dem")
replace cand_name = "Lasry" if regexm(cand_fe, "s3_dem")
replace cand_name = "Nelson" if regexm(cand_fe, "s4_dem")
replace cand_name = "Kleefisch" if regexm(cand_fe, "g1_rep")
replace cand_name = "Michels" if regexm(cand_fe, "g2_rep")

drop if regexm(cand_fe, "g") & regexm(cand_fe, "dem") // no Democratic gov race
drop if regexm(cand_fe, "s") & regexm(cand_fe, "rep") // no Republican sen race

save "2_workingdata/WI_working_long.dta", replace

**# North Carolina		
use "1_rawdata/nc_raw.dta", clear

gen startdate3 = substr(startdate, 1, 10)
gen startdate2 = date(startdate3, "YMD")
drop startdate3
order startdate2, after(startdate)
format startdate2 %td

gen starttime = substr(startdate, 12, 19)
gen starttime2 = clock(starttime, "hms") / 1000
order starttime2, after(startdate2)
format starttime2 %14.0f // Seconds since midnight

gen late_response = 0
replace late_response = 1 if startdate2 > td(17may2022) // Primary was May 17.
replace late_response = 1 if startdate2 == td(17may2022) & starttime2>63000 // Polls closed 7:30pm NC = 5:30pm Mountain = 17.5*60*60 = 63000 seconds since midnight. Mountain time confirmed with Dan.

gen votecand=""
	replace votecand="s1" if regexm(vote_sen_rep,"cand1")==1  
	replace votecand="s2" if regexm(vote_sen_rep,"cand2")==1  
	replace votecand="s3" if regexm(vote_sen_rep,"cand3")==1  
	 
local encode_list nc_partyreg primaryparticipation primaryparty1 primaryparty2 primaryparty3 ///
	gender education income religion pid postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5 ///
	elect_treatcond_sen party 
	
foreach var in `encode_list' {
	rename `var' `var'_old
	encode `var'_old, gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

destring electability_*, replace

numlabel, add

lab drop gender // This is to make NC consistent with the other states.
replace gender = 4 if gender==3 // This is to make NC consistent with the other states, where there are two binary categories, taking values 2 and 3.

recode educ (5=1) (4=2) (1=3) (8=4) (2=5) (6=6) (3 7=7), gen(educ2)
lab def educ2 1 "No Diploma" 2 "HS" 3 "Associates" 4 "Some col" 5 "BA" 6 "MA" 7 "Advanced degree"
lab val educ2 educ2

local val_order postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5
foreach var in `val_order' {
	rename `var' `var'_old
	recode `var'_old (2=0 "0. Not at all likely") (3=1 "1. Not very likely ") (4=2 "2. Somewhat likely ") (1=3 "3. Certain to"), gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

recode primaryparticipation (3 4 6 = 1) (1 2 5 = 0), gen(voter)
lab def voter 0 "Not likely voter" 1 "Likely voter"
lab val voter voter	

rename mc_officesen mc_office_sen
rename q96 mc_money_sen	
rename q97 mc_moderate_sen
	
rename vote_sen_rep vote_sen_rep2
gen vote_sen_rep = ""
replace vote_sen_rep = "McCrory" if regex(vote_sen_rep2, "cand1}")
replace vote_sen_rep = "Budd" if regex(vote_sen_rep2, "cand2}")
replace vote_sen_rep = "Walker" if regex(vote_sen_rep2, "cand3}")
replace vote_sen_rep = "Eastman" if regex(vote_sen_rep2, "cand4}")
replace vote_sen_rep = "Flaherty" if regex(vote_sen_rep2, "cand5}")
replace vote_sen_rep = "Griffiths" if regex(vote_sen_rep2, "cand6}")
replace vote_sen_rep = "Harper" if regex(vote_sen_rep2, "cand7}")
replace vote_sen_rep = "Moss" if regex(vote_sen_rep2, "cand8}")
replace vote_sen_rep = "Sibhatu" if regex(vote_sen_rep2, "cand9}")
replace vote_sen_rep = "Tshiovo" if regex(vote_sen_rep2, "cand10}")
replace vote_sen_rep = "Banwart" if regex(vote_sen_rep2, "cand11}")
replace vote_sen_rep = "Brian" if regex(vote_sen_rep2, "cand12}")
replace vote_sen_rep = "Bryant" if regex(vote_sen_rep2, "cand13}")
replace vote_sen_rep = "Bulecza" if regex(vote_sen_rep2, "cand14}")
replace vote_sen_rep = "Other" if regex(vote_sen_rep2, "Other")

order vote_sen_rep, after(vote_sen_rep2)
drop vote_sen_rep2

rename electability_sen1_1 electability_sen1
rename electability_sen2_1 electability_sen2
rename electability_sen3_1 electability_sen3
	
foreach num of numlist 1/3 {
	gen t_exp_y_s`num' = 0
	gen t_exp_n_s`num' = 0
	gen t_money_y_s`num' = 0
	gen t_money_n_s`num' = 0
	gen t_moderate_y_s`num' = 0
	gen t_moderate_n_s`num' = 0
	}
	
* This is to standardize with how conditions are coded in other states. (No experience treatment in NC, but is is =2 in other states)
recode elect_treatcond_sen (1=1 "1. control") (2=3 "3. ideology") (3=4 "4. money"), gen(elect_treatcond_sen2)
drop elect_treatcond_sen
rename elect_treatcond_sen2 elect_treatcond_sen

* Code treatment / candidate combinations
/* Note: 
elect_sencand1 = McCrory
elect_sencand2 = Budd
elect_sencand3 = Walker
*/

* Code treatment dummies
replace t_money_y_s1 = 1 if elect_treatcond_sen==4	// McCrory raised money
replace t_money_y_s2 = 1 if elect_treatcond_sen==4	// Budd raised money
replace t_money_n_s3 = 1 if elect_treatcond_sen==4	// Walker did not raise money

replace t_moderate_y_s1 = 1 if elect_treatcond_sen==3 // McCrory is moderate
replace t_moderate_n_s2 = 1 if elect_treatcond_sen==3 // Budd is extreme
replace t_moderate_n_s3 = 1 if elect_treatcond_sen==3 // Walker is extreme

* Code MC as correct
foreach num of numlist 1/3 {
	gen mc_office_s`num' = 0
	gen mc_money_s`num' = 0
	gen mc_moderate_s`num' = 0
	gen perceived_money_s`num' = 0
	gen perceived_office_s`num' = 0
	gen perceived_moderate_s`num' = 0
	}

replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}") // mccrory correct if present
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}") // budd correct if present
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") // walker correct if present
	
replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") // mccrory correct if present
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") // budd correct if present
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") == 0 // walker correct if absent

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") // mccrory correct if present
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") == 0 // budd correct if absent
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") == 0 // walker correct if absent

** Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") 
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}")
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}") 
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}")
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}") 

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") 
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}")
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") 


foreach num of numlist 1/3 {
	rename  electability_sen`num' electability_s`num'
	rename  postvote_sen_`num' postvote_s`num'
	}
	
gen state = "NC"
gen race_fe = "NC_GOP_Senate"

drop if party==1 // Drop Democrats: there was only a Republican primary

keep responseid t_* electability* elect_treatcond_sen party state race_fe mc_* ///
	postvote* vote_sen* perceived_* pid pid_leaner pid_strength_dem ///
	pid_strength_rep voter educ2    votecand late_response ///
	gender education race birthyr ideology startdate
	
	
drop electability_sen4_1 electability_sen5_1 postvote_sen_4 postvote_sen_5 mc_office_sen mc_money_sen mc_moderate_sen 

reshape long electability_ t_money_y_ t_money_n_ t_moderate_y_ t_moderate_n_ t_exp_y_ t_exp_n_ mc_office_ mc_money_ mc_moderate_ postvote_ perceived_office_ perceived_money_ perceived_moderate_, i(responseid) j(cand) string

gen cand_fe = cand + "_rep" + "_NC" 

gen cand_name = ""
replace cand_name = "McCrory" if regexm(cand_fe, "s1_rep")
replace cand_name = "Budd" if regexm(cand_fe, "s2_rep")
replace cand_name = "Walker" if regexm(cand_fe, "s3_rep")

save "2_workingdata/NC_working_long.dta", replace


**# Ohio
use "1_rawdata/oh_raw.dta", clear

gen startdate3 = substr(startdate, 1, 10)
gen startdate2 = date(startdate3, "YMD")
drop startdate3
order startdate2, after(startdate)
format startdate2 %td

gen starttime = substr(startdate, 12, 19)
gen starttime2 = clock(starttime, "hms") / 1000
order starttime2, after(startdate2)
format starttime2 %14.0f // Seconds since midnight

gen late_response = 0
replace late_response = 1 if startdate2 > td(03may2022) // Primary was May 3.
replace late_response = 1 if startdate2 == td(03may2022) & starttime2 > 63000 // Polls closed 7:30pm OH = 5:30pm Mountain = 17.5*60*60 = 63000 seconds since midnight.

gen votecand=""
	replace votecand="s1" if regexm(vote_sen_rep,"cand1")==1 & elect_showsen=="1" & party=="Republican"
	replace votecand="s2" if regexm(vote_sen_rep,"cand2")==1 & elect_showsen=="1" & party=="Republican"
	replace votecand="s3" if regexm(vote_sen_rep,"cand3")==1 & elect_showsen=="1" & party=="Republican" 
	replace votecand="s4" if regexm(vote_sen_rep,"cand4")==1 & elect_showsen=="1" & party=="Republican" 
	replace votecand="s5" if regexm(vote_sen_rep,"cand5")==1 & elect_showsen=="1" & party=="Republican" 

	replace votecand="s1" if regexm(vote_sen_dem,"cand2")==1 & elect_showsen=="1" & party=="Democrat"
	replace votecand="s2" if regexm(vote_sen_dem,"cand1")==1 & elect_showsen=="1" & party=="Democrat" 
	replace votecand="s3" if regexm(vote_sen_dem,"cand3")==1 & elect_showsen=="1" & party=="Democrat" 
	
	replace votecand="g1" if regexm(vote_gov_rep,"cand2")==1 & elect_showgov=="1" & party=="Republican"  
	replace votecand="g2" if regexm(vote_gov_rep,"cand1")==1 & elect_showgov=="1" & party=="Republican" 
	replace votecand="g3" if regexm(vote_gov_rep,"cand3")==1 & elect_showgov=="1" & party=="Republican"  

	
drop postvote_gov_4 postvote_gov_5
	
local encode_list primaryparticipation primaryparty1 primaryparty2 primaryparty3 ///
	gender education income religion pid elect_treatcond_sen elect_treatcond_gov party ///
	postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5 ///
	postvote_gov_1 postvote_gov_2 postvote_gov_3 

foreach var in `encode_list' {
	rename `var' `var'_old
	encode `var'_old, gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

destring electability_* elect_showgov elect_showsen, replace
destring *_firstclick *_lastclick *_pagesubmit *_clickcount, replace

numlabel, add

recode educ (5=1) (4=2) (1=3) (8=4) (2=5) (6=6) (3 7=7), gen(educ2)
lab def educ2 1 "No Diploma" 2 "HS" 3 "Associates" 4 "Some col" 5 "BA" 6 "MA" 7 "Advanced degree"
lab val educ2 educ2

rename mc_officesen mc_office_sen
rename mc_officegov mc_office_gov
rename q94 mc_money_gov
rename q95 mc_moderate_gov
rename q96 mc_money_sen
rename q97 mc_moderate_sen

local val_order postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5 	postvote_gov_1 postvote_gov_2 postvote_gov_3
foreach var in `val_order' {
	rename `var' `var'_old
	recode `var'_old (2=0 "0. Not at all likely") (3=1 "1. Not very likely ") (4=2 "2. Somewhat likely ") (1=3 "3. Certain to"), gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

recode primaryparticipation (3 4 6 = 1) (1 2 5 = 0), gen(voter)
lab def voter 0 "Not likely voter" 1 "Likely voter"
lab val voter voter
	
rename vote_sen_rep vote_sen_rep2
gen vote_sen_rep = ""
replace vote_sen_rep = "Dolan" if regex(vote_sen_rep2, "cand1}")
replace vote_sen_rep = "Gibbons" if regex(vote_sen_rep2, "cand2}")
replace vote_sen_rep = "Mandel" if regex(vote_sen_rep2, "cand3}")
replace vote_sen_rep = "Timken" if regex(vote_sen_rep2, "cand4}")
replace vote_sen_rep = "Vance" if regex(vote_sen_rep2, "cand5}")
replace vote_sen_rep = "Pikita" if regex(vote_sen_rep2, "cand6}")
replace vote_sen_rep = "Patel" if regex(vote_sen_rep2, "cand7}")
replace vote_sen_rep = "Other" if regex(vote_sen_rep2, "Other")
order vote_sen_rep, after(vote_sen_rep2)
drop vote_sen_rep2

rename vote_gov_rep vote_gov_rep2
gen vote_gov_rep = ""
replace vote_gov_rep = "Blystone" if regex(vote_gov_rep2, "cand1}")
replace vote_gov_rep = "DeWine" if regex(vote_gov_rep2, "cand2}")
replace vote_gov_rep = "Renacci" if regex(vote_gov_rep2, "cand3}")
replace vote_gov_rep = "Hood" if regex(vote_gov_rep2, "cand4}")
replace vote_gov_rep = "Other" if regex(vote_gov_rep2, "Other")
order vote_gov_rep, after(vote_gov_rep2)
drop vote_gov_rep2

rename vote_sen_dem vote_sen_dem2
gen vote_sen_dem = ""
replace vote_sen_dem = "Harper" if regex(vote_sen_dem2, "cand1}")
replace vote_sen_dem = "Ryan" if regex(vote_sen_dem2, "cand2}")
replace vote_sen_dem = "Johnson" if regex(vote_sen_dem2, "cand3}")
replace vote_sen_dem = "Other" if regex(vote_sen_dem2, "Other")
order vote_sen_dem, after(vote_sen_dem2)
drop vote_sen_dem2

rename vote_gov_dem vote_gov_dem2
gen vote_gov_dem = ""
replace vote_gov_dem = "Cranley" if regex(vote_gov_dem2, "cand1}")
replace vote_gov_dem = "Whaley" if regex(vote_gov_dem2, "cand2}")
replace vote_gov_dem = "Other" if regex(vote_gov_dem2, "Other")
order vote_gov_dem, after(vote_gov_dem2)
drop vote_gov_dem2

foreach num of numlist 1/5 {
	rename electability_sen`num'_1 electability_sen`num'
	rename electability_gov`num'_1 electability_gov`num'
	}
	
foreach num of numlist 1/5 {
	gen t_exp_y_s`num' = 0
	gen t_exp_n_s`num' = 0
	gen t_money_y_s`num' = 0
	gen t_money_n_s`num' = 0
	gen t_moderate_y_s`num' = 0
	gen t_moderate_n_s`num' = 0
	}

foreach num of numlist 1/5 {
	gen t_exp_y_g`num' = 0
	gen t_exp_n_g`num' = 0
	gen t_money_y_g`num' = 0
	gen t_money_n_g`num' = 0
	gen t_moderate_y_g`num' = 0
	gen t_moderate_n_g`num' = 0
	}
	
/* Note:
elect_sencand1 = Matt Dolan
elect_sencand2 = Mike Gibbons
elect_sencand3 = Josh Mandel
elect_sencand4 = Jane Timken
elect_sencand5 = J.D. Vance */

replace t_exp_y_s1 = 1 if elect_treatcond_sen==2 & party==2 // Dolan has experience
replace t_exp_n_s2 = 1 if elect_treatcond_sen==2 & party==2 // Gibbons has no experience
replace t_exp_y_s3 = 1 if elect_treatcond_sen==2 & party==2 // Mandel has experience
replace t_exp_n_s4 = 1 if elect_treatcond_sen==2 & party==2 // Timken has no experience
replace t_exp_n_s5 = 1 if elect_treatcond_sen==2 & party==2 // Vance has no experience

replace t_moderate_y_s1 = 1 if elect_treatcond_sen==3 & party==2 // Dolan is moderate
replace t_moderate_n_s2 = 1 if elect_treatcond_sen==3 & party==2 // Gibbons is not moderate
replace t_moderate_n_s3 = 1 if elect_treatcond_sen==3 & party==2 // Mandel is not moderate
replace t_moderate_n_s4 = 1 if elect_treatcond_sen==3 & party==2 // Timken is not moderate
replace t_moderate_n_s5 = 1 if elect_treatcond_sen==3 & party==2 // Vance is not moderate

replace t_money_y_s1 = 1 if elect_treatcond_sen==4 & party==2 // Dolan raised money
replace t_money_y_s2 = 1 if elect_treatcond_sen==4 & party==2 // Gibbons raised money
replace t_money_n_s3 = 1 if elect_treatcond_sen==4 & party==2 // Mandel did not raise money
replace t_money_y_s4 = 1 if elect_treatcond_sen==4 & party==2 // Timken raised money
replace t_money_n_s5 = 1 if elect_treatcond_sen==4 & party==2 // Vance did not raise money

/* Note: 
elect_govcand1 = Mike DeWine
elect_govcand2 = Joe Blystone
elect_govcand3 = Jim Renacci */

replace t_exp_y_g1 = 1 if elect_treatcond_gov==2 & party==2 // DeWine has experience
replace t_exp_n_g2 = 1 if elect_treatcond_gov==2 & party==2 // Blystone no experience
replace t_exp_y_g3 = 1 if elect_treatcond_gov==2 & party==2 // Renacci has experience

replace t_moderate_y_g1 = 1 if elect_treatcond_gov==3 & party==2 // DeWine is moderate
replace t_moderate_n_g2 = 1 if elect_treatcond_gov==3 & party==2 // Blystone not moderate
replace t_moderate_n_g3 = 1 if elect_treatcond_gov==3 & party==2 // Renacci not moderate

replace t_money_y_g1 = 1 if elect_treatcond_gov==4 & party==2 // DeWine raised money 
replace t_money_n_g2 = 1 if elect_treatcond_gov==4 & party==2 // Blystone no money
replace t_money_n_g3 = 1 if elect_treatcond_gov==4 & party==2 // Renacci no money

/* Note:
Dems
elect_sencand1 = Tim Ryan
elect_sencand2 = Morgan Harper
elect_sencand3 = Traci Johnson
*/

replace t_exp_y_s1 = 1 if elect_treatcond_sen==2 & party==1 // Ryan has experience
replace t_exp_n_s2 = 1 if elect_treatcond_sen==2 & party==1 // Harper no experience
replace t_exp_n_s3 = 1 if elect_treatcond_sen==2 & party==1 // Johnson no experience

replace t_moderate_y_s1 = 1 if elect_treatcond_sen==3 & party==1 // Ryan moderate
replace t_moderate_n_s2 = 1 if elect_treatcond_sen==3 & party==1 // Harper not moderate
replace t_moderate_y_s3 = 1 if elect_treatcond_sen==3 & party==1 // Johnson moderate

replace t_money_y_s1 = 1 if elect_treatcond_sen==4 & party==1 // Ryan yes money
replace t_money_n_s2 = 1 if elect_treatcond_sen==4 & party==1 // Harper no money
replace t_money_n_s3 = 1 if elect_treatcond_sen==4 & party==1 // Johnson no money

* Code MC as correct
foreach num of numlist 1/5 {
	gen mc_office_s`num' = 0
	gen mc_money_s`num' = 0
	gen mc_moderate_s`num' = 0
	gen mc_office_g`num' = 0
	gen mc_money_g`num' = 0
	gen mc_moderate_g`num' = 0
	gen perceived_money_s`num' = 0
	gen perceived_office_s`num' = 0
	gen perceived_moderate_s`num' = 0
	gen perceived_money_g`num' = 0
	gen perceived_office_g`num' = 0
	gen perceived_moderate_g`num' = 0
	}	
	
gen govshow_gop = 0
gen senshow_gop = 0
gen senshow_dem = 0

replace govshow_gop = 1 if party == 2 & elect_showgov == 1
replace senshow_gop = 1 if party == 2 & elect_showsen == 1
replace senshow_dem = 1 if party == 1 & elect_showsen == 1

replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_gop == 1 // Dolan correct if present
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}") == 0 & senshow_gop == 1 // Gibbons correct if absent
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_gop == 1 // Mandel correct if present
replace mc_office_s4 = 1 if regexm(mc_office_sen, "cand4}") == 0 & senshow_gop == 1 // Timken correct if absent
replace mc_office_s5 = 1 if regexm(mc_office_sen, "cand5}") == 0 & senshow_gop == 1 // Vance correct if absent

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") & senshow_gop == 1 // Dolan correct if present
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") == 0 & senshow_gop == 1 // Gibbons correct if absent
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") == 0 & senshow_gop == 1 // Mandel correct if absent
replace mc_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}") == 0 & senshow_gop == 1 // Timken correct if absent
replace mc_moderate_s5 = 1 if regexm(mc_moderate_sen, "cand5}") == 0 & senshow_gop == 1 // Vance correct if absent

** Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_gop == 1
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}") & senshow_gop == 1
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_gop == 1 
replace perceived_office_s4 = 1 if regexm(mc_office_sen, "cand4}") & senshow_gop == 1 
replace perceived_office_s5 = 1 if regexm(mc_office_sen, "cand5}") & senshow_gop == 1 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}")  & senshow_gop == 1
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}") & senshow_gop == 1
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}")  & senshow_gop == 1
replace perceived_money_s4 = 1 if regexm(mc_money_sen, "cand4}")  & senshow_gop == 1
replace perceived_money_s5 = 1 if regexm(mc_money_sen, "cand5}")  & senshow_gop == 1

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}")  & senshow_gop == 1
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") & senshow_gop == 1
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}")  & senshow_gop == 1
replace perceived_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}")  & senshow_gop == 1
replace perceived_moderate_s5 = 1 if regexm(mc_moderate_sen, "cand5}")  & senshow_gop == 1


replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") & senshow_gop == 1 // Dolan correct if present
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") & senshow_gop == 1 // Gibbons correct if present
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") == 0 & senshow_gop == 1 // Mandel correct if absent
replace mc_money_s4 = 1 if regexm(mc_money_sen, "cand4}") & senshow_gop == 1 // Timken correct if present
replace mc_money_s5 = 1 if regexm(mc_money_sen, "cand5}") == 0 & senshow_gop == 1 // Vance correct if absent

replace mc_office_g1 = 1 if regexm(mc_office_gov, "cand1}") & govshow_gop == 1 // Dewine correct if present
replace mc_office_g2 = 1 if regexm(mc_office_gov, "cand2}") == 0 & govshow_gop == 1 // Blystone correct if absent
replace mc_office_g3 = 1 if regexm(mc_office_gov, "cand3}") == 0 & govshow_gop == 1 // Renacci correct if present

replace mc_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}") & govshow_gop == 1 // Dewine correct if present
replace mc_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}") == 0 & govshow_gop == 1 // Blystone correct if absent
replace mc_moderate_g3 = 1 if regexm(mc_moderate_gov, "cand3}") == 0 & govshow_gop == 1 // Renacci correct if absent

replace mc_money_g1 = 1 if regexm(mc_money_gov, "cand1}") & govshow_gop == 1 // Dewine correct if present
replace mc_money_g2 = 1 if regexm(mc_money_gov, "cand2}") == 0 & govshow_gop == 1 // Blystone correct if absent
replace mc_money_g3 = 1 if regexm(mc_money_gov, "cand3}") == 0 & govshow_gop == 1 // Renacci correct if absent	

** Additional: Perceptions
replace perceived_office_g1 = 1 if regexm(mc_office_gov, "cand1}") & govshow_gop == 1 
replace perceived_office_g2 = 1 if regexm(mc_office_gov, "cand2}") & govshow_gop == 1
replace perceived_office_g3 = 1 if regexm(mc_office_gov, "cand3}") & govshow_gop == 1
replace perceived_office_g4 = 1 if regexm(mc_office_gov, "cand4}") & govshow_gop == 1
replace perceived_office_g5 = 1 if regexm(mc_office_gov, "cand5}") & govshow_gop == 1

replace perceived_money_g1 = 1 if regexm(mc_money_gov, "cand1}") & govshow_gop == 1
replace perceived_money_g2 = 1 if regexm(mc_money_gov, "cand2}") & govshow_gop == 1
replace perceived_money_g3 = 1 if regexm(mc_money_gov, "cand3}") & govshow_gop == 1
replace perceived_money_g4 = 1 if regexm(mc_money_gov, "cand4}") & govshow_gop == 1
replace perceived_money_g5 = 1 if regexm(mc_money_gov, "cand5}") & govshow_gop == 1

replace perceived_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}")  & govshow_gop == 1
replace perceived_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}") & govshow_gop == 1
replace perceived_moderate_g3 = 1 if regexm(mc_moderate_gov, "cand3}") & govshow_gop == 1
replace perceived_moderate_g4 = 1 if regexm(mc_moderate_gov, "cand4}") & govshow_gop == 1
replace perceived_moderate_g5 = 1 if regexm(mc_moderate_gov, "cand5}") & govshow_gop == 1

replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_dem == 1 // Ryan correct if present
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}") == 0 & senshow_dem == 1 // Harper correct if absent
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") == 0 & senshow_dem == 1 // Johnson correct if absent

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") & senshow_dem == 1 // Ryan correct if present
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") == 0 & senshow_dem == 1 // Harper correct if absent
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") & senshow_dem == 1 // Johnson correct if present

replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") & senshow_dem == 1 // Ryan correct if present
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") == 0 & senshow_dem == 1 // Harper correct if absent
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") == 0 & senshow_dem == 1 // Johnson correct if absent

** Additional: Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_dem == 1
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}") & senshow_dem == 1
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_dem == 1 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}")  & senshow_dem == 1
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}") & senshow_dem == 1
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}")  & senshow_dem == 1

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}")  & senshow_dem == 1
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") & senshow_dem == 1
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}")  & senshow_dem == 1


* Missing values for ones that were not shown
foreach num of numlist 1/5 {
	replace mc_office_s`num' = . if elect_showsen==0
	replace mc_moderate_s`num' = . if elect_showsen==0
	replace mc_money_s`num' = . if elect_showsen==0
	replace mc_office_g`num' = . if elect_showgov==0
	replace mc_moderate_g`num' = . if elect_showgov==0
	replace mc_money_g`num' = . if elect_showgov==0
	}

gen elect_govcand4 = "" // These just make the loop below work.
gen elect_govcand5 = ""
foreach num of numlist 1/5 {
	replace mc_office_s`num' = . if elect_sencand`num'==""
	replace mc_office_g`num' = . if elect_govcand`num'==""
	replace mc_moderate_s`num' = . if elect_sencand`num'==""
	replace mc_moderate_g`num' = . if elect_govcand`num'==""
	replace mc_money_s`num' = . if elect_sencand`num'==""
	replace mc_money_g`num' = . if elect_govcand`num'==""
	}
drop elect_govcand4 elect_govcand5

gen postvote_gov_4 = .
gen postvote_gov_5 = .

foreach num of numlist 1/5 {
	rename  electability_sen`num' electability_s`num'
	rename  electability_gov`num' electability_g`num'
	rename  postvote_sen_`num' postvote_s`num'
	rename  postvote_gov_`num' postvote_g`num'
	}
	
drop postvote_g4 postvote_g5
gen state = "OH"
gen race_fe = ""

replace race_fe = "OH_GOP_Senate" if elect_showsen==1 & party==2
replace race_fe = "OH_GOP_Governor" if elect_showgov==1 & party==2
replace race_fe = "OH_Dem_Senate" if elect_showsen==1 & party==1

keep responseid t_* electability_* elect_treatcond_* party state mc_* postvote* ///
	race_fe vote_sen_rep vote_gov_rep vote_sen_dem vote_gov_dem govshow_gop ///
	senshow_gop senshow_dem perceived_* pid pid_leaner pid_strength_dem ///
	pid_strength_rep voter educ2 votecand late_response /// 
	gender education race birthyr ideology startdate

drop mc_office_sen mc_money_sen mc_moderate_sen mc_office_gov mc_money_gov mc_moderate_gov
reshape long electability_ t_exp_y_ t_exp_n_ t_money_y_ t_money_n_ t_moderate_y_ t_moderate_n_ mc_office_ mc_money_ mc_moderate_ postvote_ perceived_office_ perceived_money_ perceived_moderate_, i(responseid) j(cand) string


gen cand_fe = cand + "_rep" + "_OH" if party==2
replace cand_fe = cand + "_dem" + "_OH" if party==1

gen cand_name = ""
replace cand_name = "Dolan" if regexm(cand_fe, "s1_rep")
replace cand_name = "Gibbons" if regexm(cand_fe, "s2_rep")
replace cand_name = "Mandel" if regexm(cand_fe, "s3_rep")
replace cand_name = "Timken" if regexm(cand_fe, "s4_rep")
replace cand_name = "Vance" if regexm(cand_fe, "s5_rep")
replace cand_name = "DeWine" if regexm(cand_fe, "g1_rep")
replace cand_name = "Blystone" if regexm(cand_fe, "g2_rep")
replace cand_name = "Renacci" if regexm(cand_fe, "g3_rep")
replace cand_name = "Ryan" if regexm(cand_fe, "s1_dem")
replace cand_name = "Harper" if regexm(cand_fe, "s2_dem")
replace cand_name = "Johnson" if regexm(cand_fe, "s3_dem")

drop if regexm(cand_fe, "g") & regexm(cand_fe, "_dem") // no Democratic governor's race
drop if regexm(cand_fe, "g4") // no 4th governor candidate
drop if regexm(cand_fe, "g5") // no 5th governor candidate
drop if regexm(cand_fe, "s4") & regexm(cand_fe, "_dem") // no 4th democratic senate candidate
drop if regexm(cand_fe, "s5") & regexm(cand_fe, "_dem") // no 5th democratic senate candidate
drop if regexm(cand_fe, "s") & govshow_gop==1
drop if regexm(cand_fe, "g") & (senshow_gop==1 | senshow_dem==1)

save "2_workingdata/OH_working_long.dta", replace

**# Pennsylvania

use "1_rawdata/pa_raw.dta", clear

gen startdate3 = substr(startdate, 1, 10)
gen startdate2 = date(startdate3, "YMD")
drop startdate3
order startdate2, after(startdate)
format startdate2 %td

gen starttime = substr(startdate, 12, 19)
gen starttime2 = clock(starttime, "hms") / 1000
order starttime2, after(startdate2)
format starttime2 %14.0f // Seconds since midnight

gen late_response = 0
replace late_response = 1 if startdate2 > td(17may2022) // Primary was May 3.
replace late_response = 1 if startdate2 == td(17may2022) & starttime2 > 64800 // Polls closed 8pm in PA = 6:00pm Mountain = 18*60*60 = 64800 seconds since midnight. This results in zero exclusions.

gen votecand=""
	replace votecand="s1" if regexm(vote_sen_rep,"cand1")==1 & elect_showsen=="1" & party=="Republican"
	replace votecand="s2" if regexm(vote_sen_rep,"cand2")==1 & elect_showsen=="1" & party=="Republican"
	replace votecand="s3" if regexm(vote_sen_rep,"cand5")==1 & elect_showsen=="1" & party=="Republican" 
	replace votecand="s4" if regexm(vote_sen_rep,"cand6")==1 & elect_showsen=="1" & party=="Republican" 
	replace votecand="s5" if regexm(vote_sen_rep,"cand7")==1 & elect_showsen=="1" & party=="Republican" 

	replace votecand="s1" if regexm(vote_sen_dem,"cand1")==1 & elect_showsen=="1" & party=="Democrat"
	replace votecand="s2" if regexm(vote_sen_dem,"cand2")==1 & elect_showsen=="1" & party=="Democrat" 
	replace votecand="s3" if regexm(vote_sen_dem,"cand4")==1 & elect_showsen=="1" & party=="Democrat" 
	
	replace votecand="g1" if regexm(vote_gov_rep,"cand1")==1 & elect_showgov=="1" & party=="Republican"  
	replace votecand="g2" if regexm(vote_gov_rep,"cand2")==1 & elect_showgov=="1" & party=="Republican" 
	replace votecand="g3" if regexm(vote_gov_rep,"cand6")==1 & elect_showgov=="1" & party=="Republican"  

local encode_list primaryparticipation primaryparty1 primaryparty2 primaryparty3 ///
	gender education income religion pid elect_treatcond_sen elect_treatcond_gov party  ///
	postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5 ///
	postvote_gov_1 postvote_gov_2 postvote_gov_3 postvote_gov_4 postvote_gov_5

foreach var in `encode_list' {
	rename `var' `var'_old
	encode `var'_old, gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}
	
destring electability_* elect_showsen elect_showgov, replace
destring *_firstclick *_lastclick *_pagesubmit *_clickcount, replace

numlabel, add

recode educ (5=1) (4=2) (1=3) (8=4) (2=5) (6=6) (3 7=7), gen(educ2)
lab def educ2 1 "No Diploma" 2 "HS" 3 "Associates" 4 "Some col" 5 "BA" 6 "MA" 7 "Advanced degree"
lab val educ2 educ2

recode primaryparticipation (3 4 6 = 1) (1 2 5 = 0), gen(voter)
lab def voter 0 "Not likely voter" 1 "Likely voter"
lab val voter voter	

rename mc_officesen mc_office_sen
rename mc_officegov mc_office_gov
rename q94 mc_money_gov
rename q95 mc_moderate_gov
rename q96 mc_money_sen
rename q97 mc_moderate_sen

local val_order postvote_sen_1 postvote_sen_2 postvote_sen_3 postvote_sen_4 postvote_sen_5 	postvote_gov_1 postvote_gov_2 postvote_gov_3 postvote_gov_4 postvote_gov_5
foreach var in `val_order' {
	rename `var' `var'_old
	recode `var'_old (2=0 "0. Not at all likely") (3=1 "1. Not very likely ") (4=2 "2. Somewhat likely ") (1=3 "3. Certain to"), gen(`var')
	order `var', after(`var'_old)
	drop `var'_old
	}

rename vote_sen_rep vote_sen_rep2
gen vote_sen_rep = ""
replace vote_sen_rep = "Barnette" if regex(vote_sen_rep2, "cand1}")
replace vote_sen_rep = "Bartos" if regex(vote_sen_rep2, "cand2}")
replace vote_sen_rep = "Bochetto" if regex(vote_sen_rep2, "cand3}")
replace vote_sen_rep = "Gale_Sean" if regex(vote_sen_rep2, "cand4}")
replace vote_sen_rep = "McCormick" if regex(vote_sen_rep2, "cand5}")
replace vote_sen_rep = "Oz" if regex(vote_sen_rep2, "cand6}")
replace vote_sen_rep = "Sands" if regex(vote_sen_rep2, "cand7}")
replace vote_sen_rep = "Other" if regex(vote_sen_rep2, "Other")
order vote_sen_rep, after(vote_sen_rep2)
drop vote_sen_rep2

rename vote_gov_rep vote_gov_rep2
gen vote_gov_rep = ""
replace vote_gov_rep = "Barletta" if regex(vote_gov_rep2, "cand1}")
replace vote_gov_rep = "Corman" if regex(vote_gov_rep2, "cand2}")
replace vote_gov_rep = "Gale_Joe" if regex(vote_gov_rep2, "cand3}")
replace vote_gov_rep = "Gerow" if regex(vote_gov_rep2, "cand4}")
replace vote_gov_rep = "Hart" if regex(vote_gov_rep2, "cand5}")
replace vote_gov_rep = "Mastriano" if regex(vote_gov_rep2, "cand6}")
replace vote_gov_rep = "McSwain" if regex(vote_gov_rep2, "cand7}")
replace vote_gov_rep = "White" if regex(vote_gov_rep2, "cand8}")
replace vote_gov_rep = "Zama" if regex(vote_gov_rep2, "cand9}")
replace vote_gov_rep = "Other" if regex(vote_gov_rep2, "Other")
order vote_gov_rep, after(vote_gov_rep2)
drop vote_gov_rep2

rename vote_sen_dem vote_sen_dem2
gen vote_sen_dem = ""
replace vote_sen_dem = "Fetterman" if regex(vote_sen_dem2, "cand1}")
replace vote_sen_dem = "Kenyatta" if regex(vote_sen_dem2, "cand2}")
replace vote_sen_dem = "Khalil" if regex(vote_sen_dem2, "cand3}")
replace vote_sen_dem = "Lamb" if regex(vote_sen_dem2, "cand4}")
replace vote_sen_dem = "Other" if regex(vote_sen_dem2, "Other")
order vote_sen_dem, after(vote_sen_dem2)
drop vote_sen_dem2

foreach num of numlist 1/5 {
	rename electability_sen`num'_1 electability_sen`num'
	rename electability_gov`num'_1 electability_gov`num'
	}

foreach num of numlist 1/5 {
	gen t_exp_y_s`num' = 0
	gen t_exp_n_s`num' = 0
	gen t_money_y_s`num' = 0
	gen t_money_n_s`num' = 0
	gen t_moderate_y_s`num' = 0
	gen t_moderate_n_s`num' = 0
	}

foreach num of numlist 1/5 {
	gen t_exp_y_g`num' = 0
	gen t_exp_n_g`num' = 0
	gen t_money_y_g`num' = 0
	gen t_money_n_g`num' = 0
	gen t_moderate_y_g`num' = 0
	gen t_moderate_n_g`num' = 0
	}

* This is to standardize with how conditions are coded across states. (No experience treatment here, but it is =2 in other states and in this state's gov. race)
recode elect_treatcond_sen (1=1 "1. control") (2=3 "3. ideology") (3=4 "4. money"), gen(elect_treatcond_sen2)
drop elect_treatcond_sen
rename elect_treatcond_sen2 elect_treatcond_sen
	
/* Note:
elect_sencand1 = Kathy Barnette
elect_sencand2 = Jeff Bartos
elect_sencand3 = David McCormick
elect_sencand4 = Mehmet Oz
elect_sencand5 = Carla Sands */

replace t_moderate_n_s1 = 1 if elect_treatcond_sen==3 & party==2 // Barnette is not moderate
replace t_moderate_n_s2 = 1 if elect_treatcond_sen==3 & party==2 // Bartos is not moderate
replace t_moderate_y_s3 = 1 if elect_treatcond_sen==3 & party==2 // McCormick is moderate
replace t_moderate_y_s4 = 1 if elect_treatcond_sen==3 & party==2 // Oz is moderate
replace t_moderate_n_s5 = 1 if elect_treatcond_sen==3 & party==2 // Sands is not moderate

replace t_money_n_s1 = 1 if elect_treatcond_sen==4 & party==2 // Barnette no money
replace t_money_n_s2 = 1 if elect_treatcond_sen==4 & party==2 // Bartos no money
replace t_money_y_s3 = 1 if elect_treatcond_sen==4 & party==2 // McCormick yes money
replace t_money_y_s4 = 1 if elect_treatcond_sen==4 & party==2 // Oz yes money
replace t_money_n_s5 = 1 if elect_treatcond_sen==4 & party==2 // Sands no money

/* Note:
elect_govcand1 = Lou Barletta
elect_govcand2 = Jake Corman
elect_govcand3 = Doug Mastriano
elect_govcand4 = Bill McSwain
elect_govcand5 = David White */

replace t_exp_y_g1 = 1 if elect_treatcond_gov==2 & party==2 // Barletta has experience
replace t_exp_y_g2 = 1 if elect_treatcond_gov==2 & party==2 // Corman has experience
replace t_exp_y_g3 = 1 if elect_treatcond_gov==2 & party==2 // Mastriano has experience
replace t_exp_n_g4 = 1 if elect_treatcond_gov==2 & party==2 // McSwain no experience
replace t_exp_y_g5 = 1 if elect_treatcond_gov==2 & party==2 // White has experience

replace t_moderate_y_g1 = 1 if elect_treatcond_gov==3 & party==2 // Barletta moderate
replace t_moderate_y_g2 = 1 if elect_treatcond_gov==3 & party==2 // Corman moderate
replace t_moderate_n_g3 = 1 if elect_treatcond_gov==3 & party==2 // Mastriano extreme
replace t_moderate_y_g4 = 1 if elect_treatcond_gov==3 & party==2 // McSwain moderate 
replace t_moderate_y_g5 = 1 if elect_treatcond_gov==3 & party==2 // White moderate

replace t_money_n_g1 = 1 if elect_treatcond_gov==4 & party==2 // Barletta no money
replace t_money_n_g2 = 1 if elect_treatcond_gov==4 & party==2 // Corman no money
replace t_money_n_g3 = 1 if elect_treatcond_gov==4 & party==2 // Mastriano no money
replace t_money_n_g4 = 1 if elect_treatcond_gov==4 & party==2 // McSwain no money
replace t_money_y_g5 = 1 if elect_treatcond_gov==4 & party==2 // White yes money

/* Note:
elect_sencand1 = John Fetterman
elect_sencand2 = Malcom Kenyatta
elect_sencand3 = Conor Lamb */

replace t_moderate_n_s1 = 1 if elect_treatcond_sen==3 & party==1 // Fetterman extreme
replace t_moderate_n_s2 = 1 if elect_treatcond_sen==3 & party==1 // Kenyatta extreme
replace t_moderate_y_s3 = 1 if elect_treatcond_sen==3 & party==1 // Lamb moderate

replace t_money_y_s1 = 1 if elect_treatcond_sen==4 & party==1 // Fetterman money
replace t_money_n_s2 = 1 if elect_treatcond_sen==4 & party==1 // Kenyatta no money
replace t_money_n_s3 = 1 if elect_treatcond_sen==4 & party==1 // Lamb no money

* Code MC as correct
foreach num of numlist 1/5 {
	gen mc_office_s`num' = 0
	gen mc_money_s`num' = 0
	gen mc_moderate_s`num' = 0
	gen mc_office_g`num' = 0
	gen mc_money_g`num' = 0
	gen mc_moderate_g`num' = 0
	gen perceived_money_s`num' = 0
	gen perceived_office_s`num' = 0
	gen perceived_moderate_s`num' = 0
	gen perceived_money_g`num' = 0
	gen perceived_office_g`num' = 0
	gen perceived_moderate_g`num' = 0
	}	
	
gen govshow_gop = 0
gen senshow_gop = 0
gen senshow_dem = 0

replace govshow_gop = 1 if party == 2 & elect_showgov == 1
replace senshow_gop = 1 if party == 2 & elect_showsen == 1
replace senshow_dem = 1 if party == 1 & elect_showsen == 1

replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}") == 0 & senshow_gop == 1 // Barnette correct if absent
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}") == 0 & senshow_gop == 1 // Bartos correct if  absent
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") == 0 & senshow_gop == 1 // McCormick correct if  absent
replace mc_office_s4 = 1 if regexm(mc_office_sen, "cand4}") == 0 & senshow_gop == 1 // Oz correct if  absent
replace mc_office_s5 = 1 if regexm(mc_office_sen, "cand5}") == 0 & senshow_gop == 1 // Sands correct if  absent

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") == 0 & senshow_gop == 1 // Barnette correct if absent
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") == 0 & senshow_gop == 1 // Bartos correct if absent
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") & senshow_gop == 1 // McCormick correct if present
replace mc_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}") & senshow_gop == 1 // Oz correct if present
replace mc_moderate_s5 = 1 if regexm(mc_moderate_sen, "cand5}") == 0 & senshow_gop == 1 // Sands correct if absent

replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") == 0 & senshow_gop == 1 //Barnette  correct if absent
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") == 0 & senshow_gop == 1 // Bartos correct if absent
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") & senshow_gop == 1 // McCormick correct if present
replace mc_money_s4 = 1 if regexm(mc_money_sen, "cand4}") & senshow_gop == 1 // Oz correct if present
replace mc_money_s5 = 1 if regexm(mc_money_sen, "cand5}") == 0 & senshow_gop == 1 // Sands correct if absent

** Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_gop == 1
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}") & senshow_gop == 1
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_gop == 1 
replace perceived_office_s4 = 1 if regexm(mc_office_sen, "cand4}") & senshow_gop == 1 
replace perceived_office_s5 = 1 if regexm(mc_office_sen, "cand5}") & senshow_gop == 1 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}")  & senshow_gop == 1
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}") & senshow_gop == 1
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}")  & senshow_gop == 1
replace perceived_money_s4 = 1 if regexm(mc_money_sen, "cand4}")  & senshow_gop == 1
replace perceived_money_s5 = 1 if regexm(mc_money_sen, "cand5}")  & senshow_gop == 1

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}")  & senshow_gop == 1
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") & senshow_gop == 1
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}")  & senshow_gop == 1
replace perceived_moderate_s4 = 1 if regexm(mc_moderate_sen, "cand4}")  & senshow_gop == 1
replace perceived_moderate_s5 = 1 if regexm(mc_moderate_sen, "cand5}")  & senshow_gop == 1

replace mc_office_g1 = 1 if regexm(mc_office_gov, "cand1}") & govshow_gop == 1 // Barletta correct if present
replace mc_office_g2 = 1 if regexm(mc_office_gov, "cand2}") & govshow_gop == 1 // Corman correct if present
replace mc_office_g3 = 1 if regexm(mc_office_gov, "cand3}") & govshow_gop == 1 // Mastriano correct if present
replace mc_office_g4 = 1 if regexm(mc_office_gov, "cand4}") == 0 & govshow_gop == 1 // McSwain correct if absent
replace mc_office_g5 = 1 if regexm(mc_office_gov, "cand5}") & govshow_gop == 1 // White correct if present

replace mc_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}") & govshow_gop == 1 // Barletta correct if present
replace mc_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}") & govshow_gop == 1 // Corman correct if present
replace mc_moderate_g3 = 1 if regexm(mc_moderate_gov, "cand3}") == 0 & govshow_gop == 1 // Mastriano correct if absent
replace mc_moderate_g4 = 1 if regexm(mc_moderate_gov, "cand4}") & govshow_gop == 1 // McSwain correct if present
replace mc_moderate_g5 = 1 if regexm(mc_moderate_gov, "cand5}") & govshow_gop == 1 // White correct if present

replace mc_money_g1 = 1 if regexm(mc_money_gov, "cand1}") == 0 & govshow_gop == 1 //Barletta  correct if absent
replace mc_money_g2 = 1 if regexm(mc_money_gov, "cand2}") == 0 & govshow_gop == 1 // Corman correct if absent
replace mc_money_g3 = 1 if regexm(mc_money_gov, "cand3}") == 0 & govshow_gop == 1 // Mastriano correct if absent
replace mc_money_g4 = 1 if regexm(mc_money_gov, "cand4}") == 0 & govshow_gop == 1 // McSwain correct if absent
replace mc_money_g5 = 1 if regexm(mc_money_gov, "cand5}") & govshow_gop == 1 // White correct if present

** Perceptions
replace perceived_office_g1 = 1 if regexm(mc_office_gov, "cand1}") & govshow_gop == 1
replace perceived_office_g2 = 1 if regexm(mc_office_gov, "cand2}") & govshow_gop == 1
replace perceived_office_g3 = 1 if regexm(mc_office_gov, "cand3}") & govshow_gop == 1
replace perceived_office_g4 = 1 if regexm(mc_office_gov, "cand4}") & govshow_gop == 1
replace perceived_office_g5 = 1 if regexm(mc_office_gov, "cand5}") & govshow_gop == 1

replace perceived_money_g1 = 1 if regexm(mc_money_gov, "cand1}") & govshow_gop == 1
replace perceived_money_g2 = 1 if regexm(mc_money_gov, "cand2}") & govshow_gop == 1
replace perceived_money_g3 = 1 if regexm(mc_money_gov, "cand3}") & govshow_gop == 1
replace perceived_money_g4 = 1 if regexm(mc_money_gov, "cand4}") & govshow_gop == 1
replace perceived_money_g5 = 1 if regexm(mc_money_gov, "cand5}") & govshow_gop == 1

replace perceived_moderate_g1 = 1 if regexm(mc_moderate_gov, "cand1}") & govshow_gop == 1
replace perceived_moderate_g2 = 1 if regexm(mc_moderate_gov, "cand2}") & govshow_gop == 1
replace perceived_moderate_g3 = 1 if regexm(mc_moderate_gov, "cand3}") & govshow_gop == 1
replace perceived_moderate_g4 = 1 if regexm(mc_moderate_gov, "cand4}") & govshow_gop == 1
replace perceived_moderate_g5 = 1 if regexm(mc_moderate_gov, "cand5}") & govshow_gop == 1

replace mc_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_dem == 1 // Fetterman correct if present
replace mc_office_s2 = 1 if regexm(mc_office_sen, "cand2}") & senshow_dem == 1 // Kenyatta correct if present
replace mc_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_dem == 1 // Lamb correct if present

replace mc_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}") == 0 & senshow_dem == 1 // Fetterman correct if absent
replace mc_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") == 0 & senshow_dem == 1 // Kenyatta correct if absent
replace mc_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}") & senshow_dem == 1 // Lamb correct if present

replace mc_money_s1 = 1 if regexm(mc_money_sen, "cand1}") & senshow_dem == 1 // Fetterman correct if present
replace mc_money_s2 = 1 if regexm(mc_money_sen, "cand2}") == 0 & senshow_dem == 1 // Kenyatta correct if absent
replace mc_money_s3 = 1 if regexm(mc_money_sen, "cand3}") == 0 & senshow_dem == 1 // Lamb correct if absent

** Perceptions
replace perceived_office_s1 = 1 if regexm(mc_office_sen, "cand1}") & senshow_dem == 1
replace perceived_office_s2 = 1 if regexm(mc_office_sen, "cand2}") & senshow_dem == 1
replace perceived_office_s3 = 1 if regexm(mc_office_sen, "cand3}") & senshow_dem == 1 

replace perceived_money_s1 = 1 if regexm(mc_money_sen, "cand1}")  & senshow_dem == 1
replace perceived_money_s2 = 1 if regexm(mc_money_sen, "cand2}") & senshow_dem == 1
replace perceived_money_s3 = 1 if regexm(mc_money_sen, "cand3}")  & senshow_dem == 1

replace perceived_moderate_s1 = 1 if regexm(mc_moderate_sen, "cand1}")  & senshow_dem == 1
replace perceived_moderate_s2 = 1 if regexm(mc_moderate_sen, "cand2}") & senshow_dem == 1
replace perceived_moderate_s3 = 1 if regexm(mc_moderate_sen, "cand3}")  & senshow_dem == 1


* Missing values for ones that were not shown
foreach num of numlist 1/5 {
	replace mc_office_s`num' = . if elect_showsen==0
	replace mc_moderate_s`num' = . if elect_showsen==0
	replace mc_money_s`num' = . if elect_showsen==0
	replace mc_office_g`num' = . if elect_showgov==0
	replace mc_moderate_g`num' = . if elect_showgov==0
	replace mc_money_g`num' = . if elect_showgov==0
	}

foreach num of numlist 1/5 {
	replace mc_office_s`num' = . if elect_sencand`num'==""
	replace mc_office_g`num' = . if elect_govcand`num'==""
	replace mc_moderate_s`num' = . if elect_sencand`num'==""
	replace mc_moderate_g`num' = . if elect_govcand`num'==""
	replace mc_money_s`num' = . if elect_sencand`num'==""
	replace mc_money_g`num' = . if elect_govcand`num'==""
	}

foreach num of numlist 1/5 {
	rename  electability_sen`num' electability_s`num'
	rename  electability_gov`num' electability_g`num'
	rename  postvote_sen_`num' postvote_s`num'
	rename  postvote_gov_`num' postvote_g`num'
	}

gen state = "PA"
gen race_fe = ""
replace race_fe = "PA_GOP_Senate" if elect_showsen==1 & party==2
replace race_fe = "PA_GOP_Governor" if elect_showgov==1 & party==2
replace race_fe = "PA_Dem_Senate" if elect_showsen==1 & party==1

keep responseid t_* electability_* elect_treatcond_* party state mc_* ///
	postvote* race_fe vote_sen_rep vote_gov_rep vote_sen_dem govshow_gop ///
	senshow_gop senshow_dem perceived_* pid pid_leaner pid_strength_dem ///
	pid_strength_rep voter educ2 votecand late_response /// 
	gender education race birthyr ideology startdate
	
drop mc_office_sen mc_money_sen mc_moderate_sen mc_office_gov mc_money_gov mc_moderate_gov
reshape long electability_ t_exp_y_ t_exp_n_ t_money_y_ t_money_n_ t_moderate_y_ t_moderate_n_ mc_office_ mc_money_ mc_moderate_ postvote_ perceived_office_ perceived_money_ perceived_moderate_, i(responseid) j(cand) string

gen cand_fe = cand + "_rep" + "_PA" if party==2
replace cand_fe = cand + "_dem" + "_PA" if party==1

gen cand_name = "Barnette" if regexm(cand_fe, "s1_rep")
replace cand_name = "Bartos" if regexm(cand_fe, "s2_rep")
replace cand_name = "McCormick" if regexm(cand_fe, "s3_rep")
replace cand_name = "Oz" if regexm(cand_fe, "s4_rep")
replace cand_name = "Sands" if regexm(cand_fe, "s5_rep")
replace cand_name = "Barletta" if regexm(cand_fe, "g1_rep")
replace cand_name = "Corman" if regexm(cand_fe, "g2_rep")
replace cand_name = "Mastriano" if regexm(cand_fe, "g3_rep")
replace cand_name = "McSwain" if regexm(cand_fe, "g4_rep")
replace cand_name = "White" if regexm(cand_fe, "g5_rep")
replace cand_name = "Fetterman" if regexm(cand_fe, "s1_dem")
replace cand_name = "Kenyatta" if regexm(cand_fe, "s2_dem")
replace cand_name = "Lamb" if regexm(cand_fe, "s3_dem")

drop if regexm(cand_fe, "g") & regexm(cand_fe, "_dem") // no Democratic governor's race
drop if regexm(cand_fe, "s4") & regexm(cand_fe, "_dem") // no 4th Dem senate candidate
drop if regexm(cand_fe, "s5") & regexm(cand_fe, "_dem") // no 5th Dem senate candidate
drop if regexm(cand_fe, "s") & govshow_gop==1
drop if regexm(cand_fe, "g") & (senshow_gop==1 | senshow_dem==1)

save "2_workingdata/PA_working_long.dta", replace

**# Combine states
clear all

use "2_workingdata/NC_working_long.dta"
append using "2_workingdata/OH_working_long.dta"
append using "2_workingdata/PA_working_long.dta"
append using "2_workingdata/WI_working_long.dta"

lab drop elect_treatcond_sen2
lab val elect_treatcond_sen elect_treatcond_gov

order vote_*, after(cand)
order cand_name, after (responseid)

order elect_treatcond_gov, after(elect_treatcond_sen)

encode responseid, gen(responseid2)
order responseid2, after(responseid)
drop responseid
rename responseid2 responseid

encode cand_fe, gen(cand_fe2)
drop cand_fe
rename cand_fe2 cand_fe

encode cand_name, gen(cand_name2)
order cand_name2, after(cand_name)
encode state, gen(state2)
encode race_fe, gen(race_fe2)

order race_fe2, after(race_fe)
order state2, after(state)

gen informed_office = 0
gen informed_moderate = 0
gen informed_money = 0

replace informed_office = 1 if elect_treatcond_sen == 2 & regexm(race_fe, "Senate")
replace informed_moderate = 1 if elect_treatcond_sen == 3 & regexm(race_fe, "Senate")
replace informed_money = 1 if elect_treatcond_sen == 4 & regexm(race_fe, "Senate")
replace informed_office = 1 if elect_treatcond_gov == 2 & regexm(race_fe, "Gov")
replace informed_moderate = 1 if elect_treatcond_gov == 3 & regexm(race_fe, "Gov")
replace informed_money = 1 if elect_treatcond_gov == 4 & regexm(race_fe, "Gov")

* Partisanship
gen pid7 = .
replace pid7 = 1 if pid==1 & pid_strength_dem=="Strong Democrat"
replace pid7 = 2 if pid==1 & pid_strength_dem=="Not so strong Democrat"
replace pid7 = 3 if pid==2 & pid_leaner=="Closer to Democratic"
replace pid7 = 4 if pid==2 & pid_leaner=="Neither"
replace pid7 = 5 if pid==2 & pid_leaner=="Closer to Republican"
replace pid7 = 6 if pid==5 & pid_strength_rep=="Not so strong Republican"
replace pid7 = 7 if pid==5 & pid_strength_rep=="Strong Republican"

gen pure_ind = 1 if pid7==4
gen leaner = 1 if pid7==3 | pid7==5
gen weak_partisan = 1 if pid7==2 | pid7==6
gen strong_partisan = 1 if pid7==1 | pid7==7

lab def pid7 1 "StrD" 2 "WeakD" 3 "LeanD" 4 "PureInd" 5 "LeanR" 6 "WeakR" 7 "StrR"
lab val pid7 pid7

** Create variables for Candidate Characteristics
bysort cand_name: egen moderate=max(t_moderate_y_)
bysort cand_name: egen experienced=max(t_exp_y_)
bysort cand_name: egen money=max(t_money_y_)

gen controlcond = 1
foreach var in t_exp_y_ t_exp_n_ t_money_y_ t_money_n_ t_moderate_y_ t_moderate_n_ {
	replace controlcond = 0 if `var'==1
	}

save "2_workingdata/working_long.dta", replace
